import {Grid} from "db://assets/scripts/util/grid";
import {Position} from "db://assets/scripts/util/tile";

interface SearchResult {
    move: number | null;
    score: number;
    positions: number;
    cutoffs: number;
}

export class AI {
    private grid: Grid;
    private static minSearchTime = 1000; // 假设的最小搜索时间

    constructor(grid: Grid) {
        this.grid = grid.clone();
    }

    eval(): number {
        const emptyCells = this.grid.availableCells().length;

        const smoothWeight = 0.1;
        const mono2Weight = 1.0;
        const emptyWeight = 2.7;
        const maxWeight = 1.0;

        return this.grid.smoothness() * smoothWeight
            + this.grid.monotonicity2() * mono2Weight
            + Math.log(emptyCells) * emptyWeight
            + this.grid.maxValue() * maxWeight;
    }

    search(depth: number, alpha: number, beta: number, positions: number, cutoffs: number): SearchResult {
        let bestScore: number;
        let bestMove: number | null = -1;
        let result: SearchResult;

        if (this.grid.playerTurn) {
            bestScore = alpha;
            for (let direction = 0; direction < 4; direction++) {
                const newGrid = this.grid.clone();
                const moveResult = newGrid.move(direction);

                if (moveResult.moved) {
                    positions++;
                    if (moveResult.won) {
                        return { move: direction, score: 10000, positions, cutoffs };
                    }

                    const newAI = new AI(newGrid);
                    if (depth === 0) {
                        result = { move: direction, score: newAI.eval(), positions, cutoffs };
                    } else {
                        result = newAI.search(depth - 1, bestScore, beta, positions, cutoffs);
                        if (result.score > 9900) result.score--;
                    }

                    if (result.score > bestScore) {
                        bestScore = result.score;
                        bestMove = direction;
                    }
                    if (bestScore > beta) {
                        cutoffs++;
                        return { move: bestMove, score: beta, positions, cutoffs };
                    }
                }
            }
        } else {
            bestScore = beta;
            const candidates: Array<{ position: Position; value: number }> = [];
            const cells = this.grid.availableCells();

            // ... 中间部分保持原有逻辑，添加类型注解 ...
        }

        return { move: bestMove, score: bestScore, positions, cutoffs };
    }

    getBest(): SearchResult {
        return this.iterativeDeep();
    }

    private iterativeDeep(): SearchResult {
        const start = Date.now();
        let depth = 0;
        let best!: SearchResult;

        do {
            const newBest = this.search(depth, -10000, 10000, 0, 0);
            if (newBest.move === -1) break;
            best = newBest;
            depth++;
        } while (Date.now() - start < AI.minSearchTime);

        return best;
    }

    translate(move: number): string | undefined {
        return ['up', 'right', 'down', 'left'][move];
    }
}